package com.lt.flashcard.view;

import java.text.DecimalFormat;
import java.util.List;

import org.zkoss.lang.OperationException;
import org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Include;
import org.zkoss.zul.Label;
import org.zkoss.zul.Textbox;

import com.lt.flashcard.model.entity.Flashcard;
import com.lt.flashcard.model.entity.FlashcardSet;
import com.lt.flashcard.model.entity.User;
import com.lt.flashcard.model.service.AAService;
import com.lt.flashcard.model.service.FlashcardSetService;
import com.lt.flashcard.model.service.Repository;

/**
 * Render the word list detail page
 * @author NNNguyen
 * @version 1.0
 */
public class WordlistDetailShowcaseCtrl extends SelectorComposer<Component> {

	private static final long serialVersionUID = 1L;
	/**
	 * Store current flashcard set
	 */
	private FlashcardSet flashcardset = null;
	
	private List<Flashcard> flashcards;
	
	private User user;
	private boolean isAllowedEdit;
	
	@Wire
	private Include lstFlashCards;
	
	
	
	@Override
	public void doBeforeComposeChildren(Component comp) throws Exception {
		super.doBeforeComposeChildren(comp);
		
		String strId = Executions.getCurrent().getParameter("id");
		try {
			int id = Integer.parseInt(strId);
			if(id > 0) {
				FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
				flashcardset = srv.findByID(new Long(id));
				flashcards = flashcardset.getFlashcards();
			}
			//Check permission
			AAService asrv = Repository.getInstance().getAAService();
			this.setUser(asrv.getCurrentUser());
			this.setAllowedEdit(asrv.checkEdit(asrv.getCurrentUser(), flashcardset));
		}
		catch (Exception e) { }
		
		if(flashcardset == null) {
			throw new OperationException("Unknow flashcardset");
		}
	}
	
	public FlashcardSet getFlashcardset() {
		return flashcardset;
	}

	public void setFlashcardset(FlashcardSet flashcardset) {
		this.flashcardset = flashcardset;
	}

	public List<FlashcardSet> getRelatedList() {
		FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
		return srv.getRelatedFlashcardSet(flashcardset);
	}
	public List<FlashcardSet> getOriginalFlashcardSet() {
		 FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
		 return srv.getOriginalFlashcardSet(flashcardset);
	 }
	
	public List<Flashcard> getFlashcards() {
		return flashcards;
	}

	/**
	 * @return the lstFlashCards
	 */
	public Include getLstFlashCards() {
		return lstFlashCards;
	}

	/**
	 * @param lstFlashCards the lstFlashCards to set
	 */
	public void setLstFlashCards(Include lstFlashCards) {
		this.lstFlashCards = lstFlashCards;
	}

	/**
	 * @param flashcards the flashcards to set
	 */
	public void setFlashcards(List<Flashcard> flashcards) {
		this.flashcards = flashcards;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @return the isAllowedEdit
	 */
	public boolean isAllowedEdit() {
		return isAllowedEdit;
	}

	/**
	 * @param isAllowedEdit the isAllowedEdit to set
	 */
	public void setAllowedEdit(boolean isAllowedEdit) {
		this.isAllowedEdit = isAllowedEdit;
	}
	/*
	 * created by Nguyen Trung Tam
	 * Date: 31/10/2012
	 */
	@Wire
	Label sotu;
	@Wire
	Textbox rating;
	@Wire
	Label sao_diem;
	@Wire
	Label voteResult;
	@Wire
	Label voteTotal;
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		String so = String.valueOf(flashcardset.getFlashcards().size());
		sotu.setValue(so);
	};
	@Listen("onChange=#rating")
	 public void saveRating(){
		 FlashcardSetService fsrv = Repository.getInstance().getFlashcardSetService();
		 fsrv.updateRating(flashcardset, new Integer(rating.getValue()));
		 DecimalFormat df = new DecimalFormat("#.##");
		 String voteResult = df.format(flashcardset.getVoteResult());
		 this.sao_diem.setValue(voteResult);
		 this.voteResult.setValue(voteResult);
		 this.voteTotal.setValue(String.valueOf(flashcardset.getVoteTotal()));
		 Clients.evalJavaScript("showNotice('" + Labels.getLabel("wordlist_detail.rating.updatesuccess") + "')");
	 }
	@Listen("onClick=#save_set_top")
	 public void saveSet(){
		FlashcardSetService fsSrv = Repository.getInstance()
		        .getFlashcardSetService();

		fsSrv.saveSetIntoSet(flashcardset);
	 }
}
